package org.linlinjava.litemall.admin.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.admin.task.ScheduledCronTaskRegistrar;
import org.linlinjava.litemall.db.domain.LitemallScheduledJobLog;
import org.linlinjava.litemall.db.service.LitemallScheduledJobLogService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class AdminJobLogSweepJob implements Job {

    private final LitemallScheduledJobLogService scheduledJobLogService;

    private final ScheduledCronTaskRegistrar scheduledCronTaskRegistrar;

    //清理任务日志 一次量是100
    public void jobLogSweep(){
        QueryWrapper<LitemallScheduledJobLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("add_time").last("LIMIT 100");
        scheduledJobLogService.getBaseMapper().delete(queryWrapper);
    }


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        this.jobLogSweep();
        scheduledCronTaskRegistrar.scheduleJobLog(jobExecutionContext);
    }
}
